﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HotDealD2.Models.ModelView;
namespace HotDealD2.Models.ModelManager
{
    public class CommentModel
    {
        private QLDataBaseDataContext dataContext;
        public CommentModel()
        {
            dataContext = new QLDataBaseDataContext();
        }

        public IList<Comment> GetAllComment()
        {
            var listComment = from list in dataContext.Comments select list;
            return listComment.ToList();
        }

        public IList<Comment> GetCommentByDeal(int maDeal)
        {

            IList<Comment> listcomment = (from comment in dataContext.Comments
                                                 where comment.MaDeal == maDeal
                                                 select comment ).ToList();
            return listcomment.ToList();
        }

        public Comment GetComment(int id)
        {
            return dataContext.Comments.Single(tv => tv.MaComment == id);
        }

        public bool InsertComment(CommentView comment)
        {
            try
            {
                Comment tempComment = new Comment();

                tempComment.MaDeal = comment.MaDeal;
                tempComment.Username = comment.Username;
                tempComment.NoiDung = comment.NoiDung;


                dataContext.Comments.InsertOnSubmit(tempComment);
                dataContext.SubmitChanges();

                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool UpdateComment(Comment comment)
        {
            try
            {
                Comment tempComment = dataContext.Comments.Single(d => d.MaComment == comment.MaComment);

                tempComment.MaDeal = comment.MaDeal;
                tempComment.MaComment = comment.MaComment;
                tempComment.Username = comment.Username;
                tempComment.NoiDung = comment.NoiDung;



                dataContext.SubmitChanges();

                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool DeleteComment(int id)
        {
            try
            {
            
                Comment comment = (from d in dataContext.Comments
                                    where d.MaComment == id
                                    select d).First();

                dataContext.Comments.DeleteOnSubmit(comment);
                dataContext.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public IList<Comment> SearchComment(string username)
        {

            var listComment = from o in dataContext.Comments where (o.ThanhVien.Username.Contains(username)) select o;
            return listComment.ToList();
        }
    }
}